package com.fhsk.file.manager.controller;

import com.fhsk.file.business.entity.dto.user.UserDto;
import com.fhsk.file.common.constants.Constants;
import com.fhsk.file.common.exception.AuthenticationException;
import com.fhsk.file.common.vo.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * web层通用数据处理
 *
 * @author LENOVO
 */
@Component
public class BaseController {

    @Autowired
    HttpServletRequest request;

    public static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    public void startPage() {
        //获取分页以及排序设置
        String pageStr = request.getParameter(Constants.PAGE);
        String limitStr = request.getParameter(Constants.LIMIT);
        String orderByColumnStr = request.getParameter(Constants.ORDER_BY_COLUMN);
        String isAscStr = request.getParameter(Constants.IS_ASC);

        Integer page = StringUtils.isNotBlank(pageStr) ? Integer.parseInt(pageStr) : 1;
        Integer limit = StringUtils.isNotBlank(limitStr) ? Integer.parseInt(limitStr) : 10;
        String order = StringUtils.isNotBlank(isAscStr) && Boolean.parseBoolean(isAscStr) ? " asc" : " desc";
        //拼接获取排序以及验证排序sql是否正确
        String orderBy = StringUtils.isNotBlank(orderByColumnStr) && isValidOrderBySql(orderByColumnStr) ? orderByColumnStr + order : "";

        PageHelper.startPage(page, limit, orderBy);
    }

    /*
     * 获取当前登录的用户
     * */
    public UserDto getCurrentUser() {
        UserDto user = (UserDto) request.getAttribute("currentUser");
        //查询roleId
        if (user == null) {
            throw new AuthenticationException(406, "您的登录已失效，请退出重新登录！");
        }
        return user;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? success() : error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result) {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success() {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error() {
        return AjaxResult.fail();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message) {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message) {
        return AjaxResult.fail(message);
    }

    /*
     * 返回通用分页内容
     * */
    public <T> AjaxResult getDataTable(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>(4);

        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        List<T> busList = pageInfo.getList();
        int size = pageInfo.getSize();

        map.put("total", total);
        map.put("list", busList);
        map.put("count", size);
        map.put("page", pageNum);

        return AjaxResult.success(map);
    }

    /**
     * 验证 order by 语法是否符合规范
     */
    public static boolean isValidOrderBySql(String value) {
        return value.matches(Constants.SQL_PATTERN);
    }

}
